﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ribbon.Web.Helper;

[assembly: WebResource("Ribbon.Web.Assets.Ribbon.css", "text/css", PerformSubstitution = true)]

[assembly: WebResource("Ribbon.Web.Assets.FileMenu_Background_blue.PNG", "image/png")]
[assembly: WebResource("Ribbon.Web.Assets.FileMenu_Background_green.PNG", "image/png")]
[assembly: WebResource("Ribbon.Web.Assets.FileMenu_Background_orange.PNG", "image/png")]
[assembly: WebResource("Ribbon.Web.Assets.FileMenu_Background_purple.PNG", "image/png")]
[assembly: WebResource("Ribbon.Web.Assets.Ribbon_Background.png", "image/png")]
[assembly: WebResource("Ribbon.Web.Assets.tab-line.png", "image/png")]
namespace Ribbon.Web
{
    [ToolboxData("<{0}:Ribbon runat=server></{0}:Ribbon>")]
    [ParseChildren(true)]
    public class RibbonContainer : CompositeControl
    {
        #region [ Fields ]
        private CustomTabContainer _tabContainer;
        #endregion

        #region [ Constructor ]
        public RibbonContainer()
        {
            _tabContainer = new CustomTabContainer(RenderMenu);
            Zones = new ControlCollection<RibbonContainer, RibbonZone>(this);
            Menu = new RibbonMenu();
        }
        #endregion

        #region [ public properties ]
        [DefaultValue(0)]
        [Category("Apperance")]
        public int ActiveTabIndex
        {
            set
            {
                this._tabContainer.ActiveTabIndex = value;
            }
            get
            {
                return this._tabContainer.ActiveTabIndex;
            }
        }
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Ribbon.Web.Helper.ControlCollection<RibbonContainer, RibbonZone> Zones { private set; get; }
        
        #region [ Menu Properties ]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public RibbonMenu Menu { private set; get; }
      
        public bool ShowMenu
        {
            set
            {
                this.ViewState["ShowMenu"] = value;
                this._tabContainer.ShowMenu = value;
            }
            get
            {
                var obj = this.ViewState["ShowMenu"];
                return obj == null ? true : (bool)obj;
            }
        }
        [DefaultValue(Ribbon.Web.MenuColor.Green)]
        public MenuColor MenuColor
        {
            set
            {
                this.Menu.Color = value;
            }
            get
            {
                return this.Menu.Color;
            }
        }
        public string MenuText
        {
            set
            {
                this.Menu.Text = value;
            }
            get
            {
                return this.Menu.Text;
            }
        }
        #endregion
        #endregion

        #region [ Remove unused tags ]
        public override void RenderBeginTag(HtmlTextWriter writer)
        {

        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {

        }
        #endregion

        #region [ Helpers ]
        private void RenderMenu(HtmlTextWriter htmlTextWriter)
        {
            // this means that this control dosent belog the control hierarchy whish is totaly wrong.. 
            // TODO: try to use ITemplate in custom tab control
            this.Menu.RenderControl(htmlTextWriter);
        }
        #endregion

        #region [ Methods ]
        protected override void CreateChildControls()
        {
            this.Controls.Add(_tabContainer);

            // zones
            int index = 0;
            foreach (RibbonZone zone in Zones)
            {
                if (string.IsNullOrEmpty(zone.ID)) zone.ID = "zone" + (index++);
                var tabPanel = new CustomTabPanel();
                tabPanel.ContentTemplate = DynamicTemplate.BuildUp(container => container.Controls.Add(zone));
                tabPanel.HeaderText = zone.HeaderText;

                this._tabContainer.Tabs.Add(tabPanel);
            }
            // main tab control
            _tabContainer.CssClass = "SBRibbon-theme";
            this.ActiveTabIndex = 0;
        }
        protected override void OnInit(EventArgs e)
        {
            EnsureChildControls();
            
            base.OnInit(e);

            /// add Resource file
            string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
            string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Ribbon.Web.Assets.Ribbon.css");
            LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation));
            this.Page.Header.Controls.Add(include);
        }
        #endregion
    }
}